using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Data;
using NeosSDI.Core.Manager;

namespace NeosSDI.DAL.ADO
{
    public class BaseRepository
    {
        /// <summary>
        /// Connection string
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings[WebConfig.Configuration.ConnectionStrings.DefaultConnection.ToDescription()].ConnectionString;
            }
        }

        private DatabaseQuery databaseQuery = null;
        public DatabaseQuery DatabaseQuery
        {
            get
            {
                if (databaseQuery == null)
                {
                    databaseQuery = DatabaseQuery.Load();
                }
                return databaseQuery;
            }
        }

        /// <summary>
        /// Execute a stored procedure
        /// </summary>
        /// <typeparam name="T">return type</typeparam>
        /// <param name="getItemCallback">callback</param>
        /// <returns>result of stored procedure</returns>
        public T CallStoredProcedure<T>(Func<SqlConnection, T> getItemCallback)
        {
            SqlConnection connect = null;
            try
            {
                string _connectionString = ConfigurationManager.ConnectionStrings[WebConfig.Configuration.ConnectionStrings.DefaultConnection.ToDescription()].ConnectionString;
                connect = new SqlConnection(_connectionString);
                connect.Open();
                return getItemCallback(connect);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connect.Close();
            }
        }
        /// <summary>
        /// Execute a query
        /// </summary>
        /// <typeparam name="T">return type</typeparam>
        /// <param name="getItemCallback">callback</param>
        /// <returns>result of the query</returns>

        public T CallQuery<T>(Func<SqlConnection, T> getItemCallback)
        {
            SqlConnection connect = null;
            try
            {
                string _connectionString = ConfigurationManager.ConnectionStrings[WebConfig.Configuration.ConnectionStrings.DefaultConnection.ToDescription()].ConnectionString;
                connect = new SqlConnection(_connectionString);
                connect.Open();
                return getItemCallback(connect);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connect.Close();
            }
        }

        /// <summary>
        /// Execute a query with no result
        /// </summary>
        /// <param name="getItemCallback">callback</param>
        public void CallNonQuery(Action<SqlConnection> getItemCallback)
        {
            SqlConnection connect = null;
            try
            {
                string _connectionString = ConfigurationManager.ConnectionStrings[WebConfig.Configuration.ConnectionStrings.DefaultConnection.ToDescription()].ConnectionString;
                connect = new SqlConnection(_connectionString);
                connect.Open();
                getItemCallback(connect);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connect.Close();
            }
        }
    }
}
